﻿//================================================================================
// Filename: LoadingProgressPlugin.as
// Authour: Jacky Fong
// Description:
// Handle loading progress
//
// Input Events
// - LoadingProgressPlugin_Load
// - LoadingProgressPlugin_SetPosition
// - LoadingProgressPlugin_Show
//
// Output Events
// - LoadingPlugin_LoadComplete
//================================================================================

package
{
	import flash.display.DisplayObject;
    import flash.display.Loader;
    import flash.net.URLRequest;	
	import flash.display.Sprite;
	import flash.events.*;
	import IPlugin;
	import PluginManager;
	import PluginEvent;
	
	public class LoadingProgressPlugin extends Sprite implements IPlugin
	{
		private static const _pluginName:String = "LoadingProgressPlugin";
		private var _manager:PluginManager;
		private var _loader:Loader; // loader as container
		
		public function LoadingProgressPlugin()
		{
			init();
		}
		
		public function get pluginName():String
		{
			return _pluginName;
		}
		
		public function get manager():PluginManager
		{
			return _manager;
		}
		
		public function init():void
		{
			trace( "Init " + _pluginName );
		}
		
		public function setManager( manager:PluginManager ):void
		{
			_manager = manager;
			_setupEventListeners();
		}		
		
		// listen to plugin's self operations
		private function _setupEventListeners():void
		{
			if( _manager == null ) return;
			
			_manager.addEventListener( "LoadingProgressPlugin_Load", _onLoad );
			_manager.addEventListener( "LoadingProgressPlugin_SetPosition", _onSetPosition );
			_manager.addEventListener( "LoadingProgressPlugin_Show", _onShow );
		}
		
        private function _loadComplete( evt:Event ):void
		{
			_manager.apiLog( this + " Load " + evt.target.content + " Complete: " + evt );
			_manager.dispatchEvent( new PluginEvent( "LoadingProgressPlugin_LoadComplete" ) );
        }
		
		private function _loadIoError( evt:IOErrorEvent ):void
		{
			_manager.apiLog( this + " Load IoError: " + evt );
		}
		
		private function _moveToTop():void
		{
			_manager.apiSetChildIndex( _loader, _manager.apiGetNumChildren() - 1 );
		}
		
		//==========================================================================
		// plugin API
		//==========================================================================		
		private function _onLoad( evt:PluginEvent ):void
		{
			_manager.apiLog( this + " _onLoad: " + evt );
						
			_loader = new Loader();
			_loader.contentLoaderInfo.addEventListener( Event.COMPLETE, _loadComplete );			
			_loader.contentLoaderInfo.addEventListener( IOErrorEvent.IO_ERROR, _loadIoError );
            var req:URLRequest = new URLRequest( evt.args.url );
            _loader.load( req );
			_loader.visible = false; //default hidden
			_manager.apiAddChild( _loader );
		}
		
		private function _onShow( evt:PluginEvent ):void
		{
			_manager.apiLog( this + " _onShow: " + evt );
			if( evt.args.show ) {
				_loader.visible = true;
				_moveToTop();				
			}
			else {
				_loader.visible = false;
			}
		}
		
		private function _onSetPosition( evt:PluginEvent ):void
		{
			_manager.apiLog( this + " _onSetPosition: " + evt );
			_loader.x = evt.args.x;
			_loader.y = evt.args.y;			
		}
	}
}